package com.itboye.eweilisten.me.zhanghu;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.me.login.LoginActivity;
import com.itboye.eweilisten.model.DeviceBean;
import com.itboye.eweilisten.presenter.UserPresenter;
import com.itboye.eweilisten.responsitory.UserResponsitory;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.util.widget.DeviceDelete;
import com.itboye.eweilisten.util.widget.IsStringSPUtils;
import com.itboye.eweilisten.util.widget.TimeFormat;
import com.itboye.eweilisten.volley.ResultEntity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 设备管理
 */
public class SheBeiActivity extends BaseActivity implements Observer, DeviceDelete {
    ImageView back;
    TextView tvTitle;
    //    SheBeiAdapter adapter;
    MyAdapter adapter;
    ListView shebei_list;
    List<DeviceBean> list;
    private TextView tvShebei;
    private boolean isShe = false;
    UserPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_she_bei);
        tvTitle.setText(R.string.sheibei_guanli);
        list = new ArrayList<>();
        presenter = new UserPresenter(this);
        presenter.onDevice(IsStringSPUtils.IsUid(),IsStringSPUtils.getSid());
        showProgressDialog("数据加载中...", true);


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvShebei:
                if (isShe == false) {
                    isShe = true;
                    shebei_list.setVisibility(View.GONE);
                } else {
                    isShe = false;
                    shebei_list.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    public static String toLongDateString(String dt) {
        SimpleDateFormat myFmt = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
        return myFmt.format(dt);
    }


    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        if (entity != null) {
            IsStringSPUtils.tianjia(entity);
            if (entity.getCode().equals("1111")) {
                Intent intent = new Intent(SheBeiActivity.this, LoginActivity.class);
                startActivity(intent);
                return;
            }
            if (entity.getEventType() == presenter.Device_success) {
                list = (List<DeviceBean>) entity.getData();
                Log.d("DeviceBean", list.size() + "");
                adapter = new MyAdapter(this, list, this);
                shebei_list.setAdapter(adapter);
                try {
                    closeProgressDialog();
                } catch (Exception e) {
                }
            }
            if (entity.getEventType() == presenter.DeleteDevice_success) {

                MAlert.alert(entity.getData() + "");
                adapter.notifyDataSetChanged();
                Log.d("adaper", entity.getData() + "");
                finish();
                try {
                    closeProgressDialog();
                } catch (Exception e) {
                }
            } else if (entity.getEventType() == presenter.DeleteDevice_fail) {
                adapter.notifyDataSetChanged();
                presenter.onDevice(IsStringSPUtils.IsUid(),IsStringSPUtils.getSid());
                Log.d("ssssssss", entity.getData() + "");
                try {
                    closeProgressDialog();
                } catch (Exception e) {
                }
            }


        }
    }

    @Override
    public void onDelete(final View view) {
        adapter.notifyDataSetChanged();
        new AlertDialog.Builder(this).setTitle("我的提示").setMessage("确定要删除吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        list.remove((Integer) view.getTag());
                        try {
                            // 通过程序我们知道删除了，但是怎么刷新ListView呢？
                            // 只需要重新设置一下adapter
                            adapter.notifyDataSetChanged();
                            Log.d("positionposition", list.get((Integer) view.getTag()).getToken() + "");

                            presenter.onDeleteDevice(IsStringSPUtils.getSid(), IsStringSPUtils.IsUid(), list.get((Integer) view.getTag()).getToken());

                        } catch (Exception e) {
                        }

                    }
                }).show();
    }

    public final class ViewHolder {
        public TextView title, sheibei_type, shebei_name, sheibei_time;
    }

    public class MyAdapter extends BaseAdapter implements View.OnClickListener {

        private LayoutInflater mInflater;
        private List<DeviceBean> mData;
        DeviceDelete delete;

        public MyAdapter(Context context, List<DeviceBean> mData, DeviceDelete delete) {
            this.mInflater = LayoutInflater.from(context);
            this.mData = mData;
            this.delete = delete;
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int arg0) {
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        @Override
        public View getView(final int position, View convertView,
                            ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.item_she_bei, null);
                holder.title = (TextView) convertView
                        .findViewById(R.id.sheibei_yichu);
                holder.sheibei_type = (TextView) convertView.findViewById(R.id.sheibei_type);
                holder.shebei_name = (TextView) convertView.findViewById(R.id.shebei_name);
                holder.sheibei_time = (TextView) convertView.findViewById(R.id.sheibei_time);
                convertView.setTag(holder);

            } else {

                holder = (ViewHolder) convertView.getTag();
            }
            holder.shebei_name.setText(mData.get(position).getModel() + "");
            holder.sheibei_time.setText(TimeFormat.DateFormat(mData.get(position).getCreate_time()));
            holder.title.setOnClickListener(this);
            holder.title.setTag(position);
            String remove = mData.get(position).getToken();
            Log.d("modelmodel", mData.get(position).getModel() + "");
            if (remove.equals(UserResponsitory.getDeviceToken())) {
                holder.sheibei_type.setVisibility(View.VISIBLE);
                holder.title.setVisibility(View.GONE);
            } else {
                holder.sheibei_type.setVisibility(View.GONE);
                holder.title.setVisibility(View.VISIBLE);
            }
            return convertView;
        }
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.sheibei_yichu:
                    delete.onDelete(v);
                    break;
            }
        }
    }
}
